package com.shi.yibanjia.entity.vo;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

@Data
public class PageVO {
    private Long total;
    private List list;

    public static PageVO of(Page page){
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        List records = page.getRecords();
        pageVO.setList(records);
        return pageVO;
    }
    public static PageVO of(Page page,Class clazz){
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        List records = page.getRecords();
        pageVO.setList(Collections.singletonList(BeanUtil.copyToList(records, clazz)));
        return pageVO;
    }

    public static <PO,VO> PageVO of(Page<PO> page, Function<PO,VO> converter){
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        List records = page.getRecords();
        pageVO.setList(records.stream().map(converter).toList());
        return pageVO;
    }
}
